package tk.mybatis.springboot.api.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import net.sf.json.JSONObject;
import tk.mybatis.springboot.model.Commodity;
import tk.mybatis.springboot.model.CountermanInfo;
import tk.mybatis.springboot.model.Message;
import tk.mybatis.springboot.model.TOrderExtend;
import tk.mybatis.springboot.model.TUser;
import tk.mybatis.springboot.model.TrankList;
import tk.mybatis.springboot.model.Usermessage;
import tk.mybatis.springboot.model.UserMessageExtend;
import tk.mybatis.springboot.service.JiYouService;
import tk.mybatis.springboot.wexin.bean.Userinfo;

/**
 * @author liujh
 */
@Controller
@RequestMapping("/jiyou")
public class JiYouController extends BaseController{
	@Autowired
	private JiYouService jiYouService;
	//微信菜单回掉
    @RequestMapping(value = "userInfo")
    @ResponseBody
    public Message getUserInfo(HttpServletRequest request, HttpServletResponse response)throws Exception {
    	HttpSession session = request.getSession();
    	Userinfo userinfo = (Userinfo)session.getAttribute("userinfo");
    	TUser user = jiYouService.getUserInfo(userinfo);
    	return success("成功",user);
    }
    
    @RequestMapping(value = "getOrders")
    @ResponseBody
    public Message getOrders(HttpServletRequest request, HttpServletResponse response)throws Exception {
    	HttpSession session = request.getSession();
    	Userinfo userinfo = (Userinfo)session.getAttribute("userinfo");
    	List<TOrderExtend> tOrders = jiYouService.getOrders(userinfo);
    	return success("成功",tOrders);
    }
    
    @RequestMapping(value = "getFollows")
    @ResponseBody
    public Message getFollows(HttpServletRequest request, HttpServletResponse response)throws Exception {
    	HttpSession session = request.getSession();
    	Userinfo userinfo = (Userinfo)session.getAttribute("userinfo");
    	List<TUser> tUsers = jiYouService.getFollows(userinfo);
    	return success("成功",tUsers);
    }
    
    @RequestMapping(value = "follow")
    @ResponseBody
    public JSONObject follow(HttpServletRequest request, HttpServletResponse response, String userId)throws Exception {
    	HttpSession session = request.getSession();
    	Userinfo userinfo = (Userinfo)session.getAttribute("userinfo");
    	Boolean bo = jiYouService.follows(userinfo, userId);
    	if(bo){	
    		return success("成功");
    	}else{
    		return error("失败");
    	}
    }
    
    @RequestMapping(value = "unfollow")
    @ResponseBody
    public JSONObject unfollow(HttpServletRequest request, HttpServletResponse response, String userId)throws Exception {
    	HttpSession session = request.getSession();
    	Userinfo userinfo = (Userinfo)session.getAttribute("userinfo");
    	Boolean bo = jiYouService.unFollows(userinfo, userId);
    	if(bo){	
    		return success("成功");
    	}else{
    		return error("失败");
    	}
    }
    
    @RequestMapping(value = "getMessage")
    @ResponseBody
    public Message getMessage(HttpServletRequest request, HttpServletResponse response, String type)throws Exception {
    	HttpSession session = request.getSession();
    	Userinfo userinfo = (Userinfo)session.getAttribute("userinfo");
    	List<UserMessageExtend> messages = jiYouService.getMessage(userinfo, type);
    	return success("成功", messages);
    }
    
    @RequestMapping(value = "getMessageInfo")
    @ResponseBody
    public Message getMessageInfo(HttpServletRequest request, HttpServletResponse response, String countermanId)throws Exception {
    	HttpSession session = request.getSession();
    	Userinfo userinfo = (Userinfo)session.getAttribute("userinfo");
    	List<UserMessageExtend> messages = jiYouService.getMessageInfo(userinfo, countermanId);
    	return success("成功", messages);
    }
    
    @RequestMapping(value = "setUserAddress")
    @ResponseBody
    public JSONObject setUserAddress(HttpServletRequest request, HttpServletResponse response, String latitude, String longitude)throws Exception {
    	HttpSession session = request.getSession();
    	Userinfo userinfo = (Userinfo)session.getAttribute("userinfo");
    	if(jiYouService.setUserAddress(userinfo, latitude, longitude)){
    		return success("成功");
    	}
    	return error("失败");
    }
    
    @RequestMapping(value = "getCounterman")
    @ResponseBody
    public Message getCounterman(HttpServletRequest request, HttpServletResponse response)throws Exception {
    	HttpSession session = request.getSession();
    	Userinfo userinfo = (Userinfo)session.getAttribute("userinfo");
    	List<TUser> countermans = jiYouService.getCounterman(userinfo);
    	return success("成功", countermans);
    }
    
    @RequestMapping(value = "getCountermanInfo")
    @ResponseBody
    public Message getCountermanInfo(HttpServletRequest request, HttpServletResponse response, String countermanId)throws Exception {
    	CountermanInfo counterman = jiYouService.getCountermanInfo(countermanId);
    	return success("成功", counterman);
    }
    
    @RequestMapping(value = "getrankingList")
    @ResponseBody
    public Message getrankingList(HttpServletRequest request, HttpServletResponse response, String type)throws Exception {
    	TrankList trankList = jiYouService.getrankingList(type);
    	return success("成功", trankList);
    }
    
    @RequestMapping(value = "getCommodity")
    @ResponseBody
    public Message getCommodity(HttpServletRequest request, HttpServletResponse response, String commodityId)throws Exception {
    	Commodity commodity = jiYouService.getCommodity(commodityId);
    	return success("成功", commodity);
    }
    
    @RequestMapping(value = "getSystemMessageInfo")
    @ResponseBody
    public Message getSystemMessageInfo(HttpServletRequest request, HttpServletResponse response, String messageId)throws Exception {
    	Usermessage userMessage = jiYouService.getSystemMessageInfo(messageId);
    	return success("成功", userMessage);
    }
}
